
package com.hlkj.pay.dto.payment;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * @author HlpayTeam
 * @date 2024/11/20 15:42
 */
public class YesNoToIntegerConverter implements Converter<Integer> {

    @Override
    public Class<Integer> supportJavaTypeKey() {
        return Integer.class; // Java 字段类型
    }

    @Override
    public com.alibaba.excel.enums.CellDataTypeEnum supportExcelTypeKey() {
        return com.alibaba.excel.enums.CellDataTypeEnum.STRING; // Excel 数据类型
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        // 从 Excel 数据转换为 Java 对象
        String value = context.getReadCellData().getStringValue();
        if ("是".equals(value)) {
            return 1;
        }
        else if ("否".equals(value)) {
            return 0;
        }
        else {
            return null; // 无法识别时返回 null
        }
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        // 将 Java 数据写回到 Excel
        Integer value = context.getValue();
        if (value == null) {
            return new WriteCellData<>("");
        }
        return new WriteCellData<>(value == 1 ? "是" : "否");
    }
}
